/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kallsonys.session;

import com.kallsonys.dao.CustomerDAO;
import com.kallsonys.service.Address;
import com.kallsonys.service.CreditCard;
import com.kallsonys.service.Customer;
import com.kallsonys.service.ElementListPage;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.ejb.Stateless;
import javax.jws.WebParam;
import javax.sql.DataSource;

/**
 *
 * @author allan
 */
@WebService(serviceName = "CustomersService")
@Stateless()
public class Customers {

    private com.kallsonys.dao.CustomerDAO dao;
    @Resource(mappedName = "jdbc/OMS", type = DataSource.class)
    DataSource dsOMS;

    @PostConstruct
    void postConstruct() {
        dao = new CustomerDAO(dsOMS);
    }

    public long CreateCustomer(Customer customer) throws Exception {
        long item = 0;
        try {

            item = dao.CreateCustomer(customer);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }

    public void UpdateCustomer(Customer customer) throws Exception {
        try {

            dao.UpdateCustomer(customer);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return;
    }

    public ElementListPage<Customer> getAllCustomer(@WebParam(name = "page") int page) throws Exception {
        ElementListPage<Customer> item = null;
        try {

            item = dao.getAllCustomer(page);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }

    public Customer getByCustomerDocument(@WebParam(name = "document") String document) throws Exception {

        Customer item = null;
        try {

            item = dao.getByCustomer(document, 1);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }

    public Customer getByCustomerId(@WebParam(name = "idCustomer") long idCustomer) throws Exception {
        Customer item = null;
        try {

            item = dao.getByCustomer(idCustomer);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }

    public Customer getByCustomerMail(@WebParam(name = "mail") String mail) throws Exception {
        Customer item = null;
        try {

            item = dao.getByCustomer(mail, 2);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }

    public String validateCustomer(@WebParam(name = "mail") String mail,
            @WebParam(name = "password") String password) throws Exception {
        Customer item = null;
        String retorno = "Usuario o Clave Invalida";
        try {

            item = dao.getByCustomer(mail, 2);
            if (item != null && item.getPassword().equals(password)) {
                retorno = "OK";
            }

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return retorno;
    }

    public long CreateCustomerAddress(@WebParam(name = "adddress") Address adddress) throws Exception {
        long item = 0;
        try {
            item = dao.CreateCustomerAddress(adddress);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }

    public void UpdateCustomerAddress(@WebParam(name = "adddress") Address adddress) throws Exception {

        try {
            dao.UpdateCustomerAddress(adddress);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return;
    }

    public List<Address> getCustomerAddress(@WebParam(name = "idCustomer") long idCustomer) throws Exception {
        List<Address> item = null;
        try {

            item = dao.getCustomerAddress(idCustomer);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }

    public long CreateCustomerCreditCard(@WebParam(name = "creditCard")  CreditCard creditCard) throws Exception {
        long item = 0;
        try {
            item = dao.CreateCustomerCreditCard(creditCard);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }

    public void UpdateCustomerCreditCard(@WebParam(name = "creditCard")  CreditCard creditCard) throws Exception {
        try {
            dao.UpdateCustomerCreditCard(creditCard);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return;
    }

    public List<CreditCard> getCustomerCreditCard(@WebParam(name = "idCustomer") long idCustomer) throws Exception {
        List<CreditCard> item = null;
        try {

            item = dao.getCustomerCreditCard(idCustomer);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }
    
    public CreditCard getCustomerCreditCardById(@WebParam(name = "idCreditCard") long idCreditCard) throws Exception {
        CreditCard item = null;
        try {

            item = dao.getCustomerCreditCardId(idCreditCard);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }
}
